<template>
  <div class="user-manage">
    <div class="query-form">
      <el-form ref="form" :inline="true" :model="queryForm">
        <el-form-item label="组织名称" prop="groupName">
          <el-input v-model="queryForm.groupName" placeholder="请输入组织名称" />
        </el-form-item>
        <el-form-item label="组织状态" prop="status">
          <el-select v-model="queryForm.status">
            <el-option :value="1" label="启用"></el-option>
            <el-option :value="0" label="停用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getGroupList">查询</el-button>
          <el-button @click="handleReset('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="base-table">
      <div class="action">
        <el-button type="primary" @click="handleAdd(1)">新增</el-button>
        <el-button type="info" plain icon="el-icon-sort" @click="toggleExpandAll">展开/折叠</el-button>
      </div>
      <el-table v-if="refreshTable" :data="groupList" row-key="groupId" :default-expand-all="isExpandAll"
        :tree-props="{ children: 'children' }" max-height="600">
        <el-table-column prop="groupName" label="组织名称" width="150" align="center"></el-table-column>
        <el-table-column v-for="item in columns" :key="item.prop" :prop="item.prop" :label="item.label"
          :width="item.width" :formatter="item.formatter" align="center">
        </el-table-column>
        <el-table-column label="操作" width="220" align="center">
          <template #default="scope">
            <el-button @click="handleAdd(2, scope.row)" type="primary" size="mini">新增</el-button>
            <el-button @click="handleEdit(scope.row)" size="mini">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="action == 'add' ? '组织新增' : '组织修改'" v-model="showModal" :close-on-click-modal="false"
      :show-close="false" width="650px" center>
      <el-form ref="dialogForm" :model="groupForm" label-width="100px" :rules="rules">
        <el-row>
          <el-col :span="24">
            <el-form-item label="父级组织" prop="parentId">
              <el-cascader v-model="groupForm.parentId" :options="groupList" :props="{
                checkStrictly: true,
                emitPath: false,
                value: 'groupId',
                label: 'groupName',
              }" clearable style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="组织名称" prop="groupName">
              <el-input v-model="groupForm.groupName" placeholder="请输入组织名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="显示顺序" prop="orderNum">
              <el-input v-model="groupForm.orderNum" placeholder="请输入显示顺序" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组织电话" prop="phone">
              <el-input v-model="groupForm.phone" placeholder="请输入组织电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组织邮箱" prop="email">
              <el-input v-model="groupForm.email" placeholder="请输入组织邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="组织状态" prop="status">
              <el-radio-group v-model="groupForm.status">
                <el-radio label="1">启用</el-radio>
                <el-radio label="0">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="groupForm.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import utils from "../utils/utils";
export default {
  name: "group",
  data() {
    return {
      queryForm: {
        status: 1,
      },
      // 组织列表
      groupList: [],
      // 重新渲染表格状态
      refreshTable: true,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      columns: [
        {
          label: "组织电话",
          prop: "phone",
        },
        {
          label: "组织邮箱",
          prop: "email",
        },
        {
          label: "组织状态",
          prop: "status",
          formatter(row, column, value) {
            return {
              1: "启用",
              0: "停用",
            }[value];
          },
        },
        {
          label: "创建时间",
          prop: "createTime",
          formatter(row, column, value) {
            return utils.formateDate(new Date(value));
          },
        },
      ],
      showModal: false,
      groupForm: {
        parentId: null,
        groupType: "1",
        status: "1",
        isFrame: "0"
      },
      action: "",
      rules: {
        groupName: [
          {
            required: true,
            message: "请输入组织名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 10,
            message: "长度在2-8个字符",
            trigger: "blur",
          },
        ],
        orderNum: [
          {
            required: true,
            message: "请输入显示顺序",
            trigger: "blur",
          },
          {
            pattern: /^[0-9]*$/,
            message: "请输入正确的显示顺序",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入组织电话",
            trigger: "blur",
          },
          {
            pattern: /1[3-9]\d{9}/,
            message: "请输入正确的组织电话",
            trigger: "blur",
          },
        ],
        email: [
          {
            message: "请输入组织邮箱",
            trigger: "blur"
          },
          {
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            message: "请输入正确的组织邮箱",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getGroupList();
  },
  methods: {
    // 组织列表初始化
    async getGroupList() {
      try {
        const { list } = await this.$api.getGroupList(this.queryForm);
        this.groupList = list;
      } catch (e) {
        throw new Error(e);
      }
    },
    // 表单重置
    handleReset(form) {
      this.$refs[form].resetFields();
    },
    // 新增组织
    handleAdd(type, row) {
      this.showModal = true;
      this.action = "add";
      if (type == 2) {
        this.groupForm.parentId = row.groupId;
      }
    },
    //展开/折叠操作
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    // 编辑组织
    handleEdit(row) {
      this.showModal = true;
      this.action = "edit";
      this.$nextTick(() => {
        Object.assign(this.groupForm, row);
      });
    },
    // 删除组织
    async handleDel(row) {
      await this.$api.groupDelete({ groupId: row.groupId });
      this.$message.success("删除成功");
      this.getGroupList();
    },
    // 提交组织
    handleSubmit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          let { action, groupForm } = this;
          let params = { ...groupForm, action };
          if (action == 'add') {
            let res = await this.$api.groupSubmit(params);
            this.$message.success("新增组织成功");
          } else {
            let res = await this.$api.groupUpdate(params);
            this.$message.success("编辑组织成功");
          }
          this.showModal = false;
          this.handleReset("dialogForm");
          this.getGroupList();
        }
      });
    },
    // 弹框关闭
    handleClose() {
      this.showModal = false;
      this.handleReset("dialogForm");
    },
  },
};
</script>

<style lang="scss">
</style>